﻿/*
 * Copyright 2010 Marc Cesarine
 * 
 * This file is part of Ssh Commander.

 * Ssh Commander is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Ssh Commander is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Ssh Commander.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;
using System.ComponentModel;
using Tamir.SharpSsh.jsch;

namespace SshCommander
{
    public class Account : INotifyPropertyChanged
    {
        private string id_v;
        private string login_v;
        private string password_v;
        private status teststatus_v;
        private string hostname_v;
        private int port_v;

        public Account()
        {
            teststatus_v = status.ARecetter;
            port_v = 22;
        }

        public string id
        {
            get
            {
                return id_v;
            }
            set
            {
                id_v = value;
                OnPropertyChanged("id");
            }
        }

        public string login
        {
            get
            {
                return login_v;
            }
            set
            {
                login_v = value;
                OnPropertyChanged("login");
            }
        }

        public string password
        {
            get
            {
                return password_v;
            }
            set
            {
                password_v = value;
                OnPropertyChanged("password");
            }
        }

        public int port
        {
            get
            {
                return port_v;
            }
            set
            {
                port_v = value;
            }
        }

        public status teststatus
        {
            get
            {
                return teststatus_v;
            }
            set
            {
                teststatus_v = value;
                OnPropertyChanged("teststatus");
            }
        }

        public enum status
        {
            OK = 1,
            KO = 2,
            ARecetter = 3,
            EnCours = 4
        }

        public string hostname
        {
            get
            {
                return hostname_v;
            }
            set
            {
                hostname_v = value;
                OnPropertyChanged("hostname");
            }
        }

        // Wrapper method for use with thread pool
        public void PasswordCheckThreadPoolCallBack(Object threadContext)
        {
            if (login_v != "")
            {
                JSch jsch = new JSch();
                Session session = null;

                try
                {
                    teststatus = Account.status.EnCours;
                    session = jsch.getSession(login_v, hostname_v, port_v);
                    session.setHost(hostname_v);
                    session.setPassword(password_v);
                    Hashtable config = new Hashtable(1);
                    config.Add("StrictHostKeyChecking", "no");
                    session.setConfig(config);

                    session.connect();

                    if (session.isConnected())
                    {
                        teststatus = Account.status.OK;
                    }
                }
                catch (Exception)
                {
                    teststatus = Account.status.KO;
                }
                finally
                {
                    if ((session != null) && (session.isConnected()))
                    {
                        session.disconnect();
                    }
                }
            }
        }

        public Account Clone()
        {
            Account result = new Account();
            result.id = id_v;
            result.login = login_v;
            result.password = password_v;
            result.teststatus = teststatus_v;

            return result;
        }

        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
